package com.yuanduan.mplus.presenter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.yuanduan.mplus.R;
import com.yuanduan.mplus.model.entity.ConsultEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cj on 2016/6/14.
 * Email:781806481@qq.com
 */
public class ConsultListViewAdapter extends BaseAdapter {
    private List<ConsultEntity> datalists=new ArrayList<ConsultEntity>();
    private Context context;

    public ConsultListViewAdapter(Context context){
        this.context=context;
    }

    @Override
    public int getCount() {
        return datalists.size();
    }

    @Override
    public Object getItem(int position) {
        return datalists.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ConsultEntity entity= (ConsultEntity) getItem(position);
        View view;
        ViewHolder viewHolder;
        if(convertView==null){
            view= LayoutInflater.from(context).inflate(R.layout.content_consult_listviewitem,null);
            viewHolder=new ViewHolder();
            viewHolder.imageView= (ImageView) view.findViewById(R.id.consult_item_iv);
            viewHolder.title= (TextView) view.findViewById(R.id.consult_item_title);
            viewHolder.content= (TextView) view.findViewById(R.id.consult_item_content);
            viewHolder.time= (TextView) view.findViewById(R.id.consult_item_time);
            view.setTag(viewHolder);
        }else{
            view=convertView;
            viewHolder= (ViewHolder) view.getTag();
        }
        viewHolder.imageView.setImageBitmap(entity.getBitmap());
        viewHolder.title.setText(entity.getTitle());
        viewHolder.content.setText(entity.getContent());
        viewHolder.time.setText(entity.getTime());
        return view;
    }

    public void setDatalists(List<ConsultEntity> lists){
        for(int i=0;i<lists.size();i++){
            datalists.add(lists.get(i));
        }
        notifyDataSetChanged();
    }

    public void setItem(ConsultEntity entity){
        if(entity!=null){
            datalists.add(entity);
        }
    }



    class ViewHolder{
        ImageView imageView;
        TextView title;
        TextView content;
        TextView time;
    }
}
